package org.ltc.alliance.controller;

import org.ltc.alliance.constant.*;
import org.ltc.alliance.model.CommentDomain;
import org.ltc.alliance.service.comment.CommentService;
import org.ltc.alliance.service.log.LogService;
import org.ltc.alliance.service.power.PowerService;
import org.ltc.alliance.utils.APIResponse;
import org.ltc.alliance.utils.GsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;
    @Autowired
    private PowerService powerService;
    @Autowired
    private LogService logService;

    @RequestMapping("/create")
    @ResponseBody
    public APIResponse<?> creatComment(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestBody String body) {
        Map<String, String> map = GsonUtils.getMapFromJson(body, HashMap.class);
        String clubId = map.get("clubId");
        String content = map.get("content");
        String visible = map.get("visible");

        String uid = "";
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(WebConst.USER_IN_COOKIE)){
                uid = cookie.getValue();
            }
        }

        CommentDomain comment = new CommentDomain();
        comment.setCid(Integer.parseInt(clubId));
        comment.setContent(content);
        comment.setUid(Integer.parseInt(uid));
        comment.setMvisible(VisibleConst.valueOf(visible));

        try{
            commentService.saveCommentInfo(comment);
            logService.addLog(LogActions.SAVE_COMMENT.getAction(),
                    null,
                    request.getRemoteAddr(),
                    Integer.parseInt(uid)
            );
        }
        catch(Exception e){
            return APIResponse.fail(e.toString());
        }

        return APIResponse.success();
    }

    @RequestMapping("/list")
    @ResponseBody
    public APIResponse<?> getCommentList(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestBody String body) {
        Map<String, String> map = GsonUtils.getMapFromJson(body, HashMap.class);
        String clubId = map.get("clubId");

        String uid = "";
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(WebConst.USER_IN_COOKIE)){
                uid = cookie.getValue();
            }
        }

        try {
            List<CommentDomain> comments = commentService.getComments(Integer.parseInt(uid),
                    Integer.parseInt(clubId), StatusConst.Comment.NORMAL);
            logService.addLog(LogActions.GET_COMMENT.getAction(),
                    null,
                    request.getRemoteAddr(),
                    Integer.parseInt(uid)
            );
            return APIResponse.success(comments);
        }
        catch (Exception e){
            return APIResponse.fail(e.toString());
        }
    }

    @RequestMapping("/revoke")
    @ResponseBody
    public APIResponse<?> revokeComment(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestBody String body) {
        Map<String, String> map = GsonUtils.getMapFromJson(body, HashMap.class);
        String mid = map.get("mid");

        /*String uid = "";
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(WebConst.USER_IN_COOKIE)){
                uid = cookie.getValue();
            }
        }*/

        try{
            commentService.alterStatusByMid(Integer.parseInt(mid), StatusConst.Comment.REVOKED);
        }
        catch(Exception e){
            return APIResponse.fail(e.toString());
        }

        return APIResponse.success();
    }
}
